# 线性回归的4种方法
# 方法3： 采用statsmodels
import os
import numpy as np
import matplotlib.pyplot as plt

import statsmodels.api as sm


def func(x, w, b):
    return x * w + b


if __name__ == '__main__':

    m = 101
    x_train = np.linspace(-1, 1, m)
    x_train = x_train.reshape(-1, 1)
    print(x_train.shape)
    y_train = 2 * x_train + np.random.randn(*x_train.shape) * 0.33

    ##########################################################方法3，采用statsmodels

    x = sm.add_constant(x_train)
    model = sm.OLS(y_train, x).fit()
    b, w = model.params

    # zz = model.predict([1, 5])
    # print(zz)

    zz = model.predict(x)
    print(model.summary())
    print('********')
    plt.figure(4)
    plt.scatter(x_train, y_train)
    plt.plot(x_train, zz, c='k')
    # plt.pause(1)
    plt.show()

    ##########################################################
    # plt.pause(0)


